using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using Perovich.GameObjects.Helpers;

namespace Perovich.GameObjects.Controls.GameStates
{
    /// <summary>
    /// A class that supports the <see cref="GameStateManager"/>. It contains the list of game states.
    /// </summary>
    public class GameStateList : Component
    {
        internal GameStateList(ICanHoldComponents game)
            : base(game)
        {
            game.Components.Add(this);
            UpdateOrder = int.MaxValue;
        }

        /// <summary>
        /// Gets the <see cref="GameState"/> for the given key.
        /// </summary>
        /// <param name="name">The name of the game state.</param>
        /// <returns>The <see cref="GameState"/> corresponding to the given key.</returns>
        public GameState this[string name]
        {
            get
            {
                try
                {
                    return states[name];
                }
                catch (KeyNotFoundException e)
                {
                    throw new KeyNotFoundException("No Game State with that name exists.", e);
                }
            }
        }

        Dictionary<string, GameState> states = new Dictionary<string,GameState>();
        bool stateChange = false;
        GameState targetState = null;
        GameState activeState = null;
        internal void SetActiveState(string name)
        {
            stateChange = true;
            foreach (var s in states)
            {
                if (s.Key.Equals(name))
                {
                    targetState = s.Value;
                }
            }
            
        }

        /// <summary>
        /// Allows the <see cref="GameStateList"/> to update the currently active state.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (stateChange)
            {
                if (activeState != null)
                    activeState.Active = false;
                targetState.Active = true;
                activeState = targetState;
                targetState = null;
                stateChange = false;
            }
            base.Update(gameTime);
        }

        internal void MakeState(string name, ICanHoldComponents game)
        {
            if (!states.ContainsKey(name))
            {
                states.Add(name, new GameState(game));
            }
            else
            {
                throw new InvalidOperationException("A Game State with that name already exists.");
            }
        }
    }

    /// <summary>
    /// A static class that manages creating and swapping between different game states.
    /// </summary>
    public static class GameStateManager
    {
        private static GameStateList list;
        /// <summary>
        /// Gets the list of <see cref="GameState"/>s.
        /// </summary>
        public static GameStateList States
        {
            get
            {
                return list;
            }
        }

        /// <summary>
        /// creates a new <see cref="GameState"/> with the given name.
        /// </summary>
        /// <param name="name">The name for the new <see cref="GameState"/>.</param>
        /// <param name="game">The game that will support this new <see cref="GameState"/>.</param>
        public static void MakeState(string name, ICanHoldComponents game)
        {
            if (list == null)
                list = new GameStateList(game);
            States.MakeState(name, game);

            Perovich.GameObjects.ComponentsExtensions.Helpers.Add(new Helper(typeof(GameStateManager).GetMethod("Reset", System.Reflection.BindingFlags.Static)));
        }

        /// <summary>
        /// Switches the active state to the named state.
        /// </summary>
        /// <param name="name">The name of the <see cref="GameState"/> to activate.</param>
        public static void SetActiveState(string name)
        {
            States.SetActiveState(name);
        }

        internal static void Reset()
        {
            list = null;
        }
    }
}
